﻿using System;

namespace ChatServer
{
    public class Message
    {
        /// <summary>
        /// Create a new instance of Message
        /// </summary>
        public Message() { }
        /// <summary>
        /// Create a new instance of Message
        /// </summary>
        public Message(Message message) : this(message.From, message.Text, message.To, message.Send) { }
        /// <summary>
        /// Create a new instance of Message
        /// </summary>
        public Message(User from, string text, User to) : this(from.UserID, text, to.UserID, DateTime.Now) { }
        /// <summary>
        /// Create a new instance of Message
        /// </summary>
        public Message(User from, string text, User to, DateTime send) : this(from.UserID, text, to.UserID, send) { }
        /// <summary>
        /// Create a new instance of Message
        /// </summary>
        public Message(int from, string text, int to) : this(from, text, to, DateTime.Now) { }
        /// <summary>
        /// Create a new instance of Message
        /// </summary>
        public Message(int from, string text, int to, DateTime send)
        {
            From = from;
            Text = text;
            To = to;
            Send = send;
        }

        /// <summary>
        /// The text of the message
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// The userID of the user that the message was send from
        /// </summary>
        public int From { get; set; }

        /// <summary>
        /// The userID of the user that the message was send to
        /// </summary>
        public int To { get; set; }

        /// <summary>
        /// The Datetime that the message was send
        /// </summary>
        public DateTime Send { get; set; }

        /// <summary>
        /// Returns a clone of the message
        /// </summary>
        public Message Clone()
        {
            return new Message(this);
        }

        /// <summary>
        /// Converts a massage to a failure message, back to the sender
        /// </summary>
        public Message ConvertToFailMessage()
        {
            Text =
                string.Format(
                    "Follwoing message was not delevired <br /> Send to: {0} <br />  Send at: {1} <br /> Message: {2}",
                    To, Send, Text);
            To = From;
            Send = DateTime.Now;
            return this;
        }
    }
}